﻿#region Copyright © 2013 Miracle Business Solutions

// All rights are reserved. Reproduction or transmission in whole or in part,
// any form or by any means, electronic, mechanical or otherwise, is prohibited
// without the prior written consent of the copyright owner.
// 
// Document:	EasyPockets\EasyPockets.cs
// Date:		12/10/2013
// Author:		Moreno Sint Hill

#endregion

using System;
using System.Runtime.InteropServices;
using Styx;
using Styx.Common;
using Styx.Common.Helpers;
using Styx.CommonBot;
using Styx.Plugins;

namespace EasyPockets
{
    public class EasyPockets : HBPlugin
    {
        #region Consts

        private const int Vanish = 1856;
        private const int BurstOfSpeed = 108212;
        private const int Preparation = 14185;
        private const int ShadowWalk = 114842;

        #endregion

        #region Timers

        private WaitTimer _Delay;
        #endregion

        #region Overrides of HBPlugin

        /// <summary>
        /// Initializes this plugin after it has been properly loaded.
        /// </summary>
        public override void Initialize()
        {
            _Delay = new WaitTimer(TimeSpan.FromSeconds(1));
            _Delay.Stop();
            base.Initialize();
        }

        /// <summary>
        /// Dispose of this plugin, cleaning up any resources it uses.
        /// </summary>
        public override void Dispose()
        {
            _Delay.Stop();
            base.Dispose();
        }

        /// <summary>
        ///     The name of this plugin.
        /// </summary>
        public override string Name
        {
            get { return @"EasyPockets"; }
        }

        /// <summary>
        ///     The author of this plugin.
        /// </summary>
        public override string Author
        {
            get { return @"Mirabis"; }
        }

        /// <summary>
        ///     The version of the plugin.
        /// </summary>
        public override Version Version
        {   //Last updated at : 10/15/2013 - 10:31 AM, by Mirabis
            get { return new Version(1, 0, 0,2); }
        }

        /// <summary>
        ///     Called everytime the engine pulses.
        /// </summary>
        public override void Pulse()
        {
            //Check if we are able to intervene
            if (!StyxWoW.IsInWorld || !StyxWoW.IsInGame) return;

            //Check if we meet the class requirements
            if (StyxWoW.Me.Class != WoWClass.Rogue) return;

            //Check if we are eating, mounted, or anything that should have higher priority
            if (StyxWoW.Me.HasAura("Food") || StyxWoW.Me.Mounted) return;

            //Check if we are in combat 
            if (StyxWoW.Me.Combat)
            {
                //Check if we can use Vanish , meaning it's off cooldown * Checking Global cooldown too, as it might report false info
                if (!SpellManager.GlobalCooldown  && SpellManager.Spells["Vanish"].Cooldown)
                    Buff(Preparation, "Reset cooldown on Vanish");

                //At this point we met the requirements, and need to do something to get out of combat
                if (_Delay.IsFinished && Buff(Vanish, "Get out of combat"))
                {
                    _Delay.Reset(); // Reset it, to make it running again, keeping us from double casting Vanish
                    Buff(ShadowWalk, "Increase stealth effectiveness");
                }
            } // Or out of combat
            else if (!StyxWoW.Me.HasAura(137573)) Buff(BurstOfSpeed, "speed is important!");
        }

        #endregion

        #region Custom Methods

        /// <summary>
        ///     Uses a buff
        /// </summary>
        /// <param name="spell">Spell to use</param>
        /// <param name="reason">Reason why we used it</param>
        /// <returns>True if cast was successful, false otherwise</returns>
        private bool Buff(int spell, string reason = "")
        {
            if (SpellManager.CanBuff(spell))
            {
                if (!SpellManager.Buff(spell))
                    return false;
                Logging.Write("[EasyPockets] Used {0} because {1}", spell, reason);
            }
            //Failed
            return false;
        }

        #endregion
    }
}